package com.ctg.itrdc.imageconsole.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.TimeUtils;
import com.ctg.itrdc.imageconsole.data.dao.IDockerImageTagDao;
import com.ctg.itrdc.imageconsole.data.dto.DockerImageIndexDTO;
import com.ctg.itrdc.imageconsole.data.entity.DockerImageTag;
import com.ctg.itrdc.imageconsole.service.IDockerImageIndexService;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.common.utils.type.StringUtils;

@Component("dockerImageIndexService")
public class DockerImageIndexServiceImpl implements IDockerImageIndexService {
	
    @Resource
    private IDockerImageTagDao dockerImageIndexDao;

    
    @Override
    public List<DockerImageTag> selectByExample(DockerImageTag entity) {
        return dockerImageIndexDao.selectByExample(entity);
    }

    @Override
    public DockerImageTag selectByPrimaryKey(DockerImageTag entity) {
        return dockerImageIndexDao.selectByPrimaryKey(entity);
    }
    
    @Override
    public int updateByPrimaryKey(DockerImageTag entity) {
        return dockerImageIndexDao.updateByPrimaryKey(entity);
    }
    
    @Override
    public int deleteByPrimaryKey(DockerImageTag entity) {
        return dockerImageIndexDao.deleteByPrimaryKey(entity);
    }

	@Override
	public PageInfo<DockerImageIndexDTO> findByImageFileId(Long imageFileId, Integer currentPage, 
			Integer perPageNum) throws Exception {
		
		if(StringUtils.isNullOrEmpty(imageFileId))
			return null;
		
		PageInfo<DockerImageIndexDTO> pi = dockerImageIndexDao.findByImageFileId(imageFileId, currentPage, perPageNum);
		List<DockerImageIndexDTO> diiList = pi.getList();
		if(diiList != null && diiList.size() > 0) {
			for(DockerImageIndexDTO dockerImageIndexDTO : diiList) {
				dockerImageIndexDTO.setStateName(this.changeStateToName(dockerImageIndexDTO.getState()));
			
				if (StringUtils.isNotEmpty(dockerImageIndexDTO.getCreateTime())) {
					Long time = TimeUtils.parse(dockerImageIndexDTO.getCreateTime()).getTime();
					dockerImageIndexDTO.setCreateTime(time.toString());
				}
				if (StringUtils.isNotEmpty(dockerImageIndexDTO.getLastUpdateTime())) {
					Long time = TimeUtils.parse(dockerImageIndexDTO.getLastUpdateTime()).getTime();
					dockerImageIndexDTO.setLastUpdateTime(time.toString());
				}
			}
		}
		return pi;
	}
	
	private String changeStateToName(String state) {
		String stateName = null;
		if("000".equals(state)) {
			stateName = "不稳定";
		}else if("001".equals(state)){
			stateName = "正常";
		}else if("002".equals(state)){
			stateName = "删除";
		}else if("003".equals(state)){
			stateName = "损坏";
		}else{
			stateName = "状态有误";
		}
		return stateName;
	}


	/* (non-Javadoc)
	 * @see com.ctg.itrdc.imageconsole.service.IDockerImageIndexService#findLatestVersion(java.lang.Long, java.lang.String)
	 */
	@Override
	public List<DockerImageTag> findByImageFileIdAndVersion(Long imageFileId,
			String version) {
		return dockerImageIndexDao.findByImageFileIdAndVersion(imageFileId, version);
	}   
}
